﻿namespace CommunicationProtocol.utils
{
    public class PixelsLocationToColumnRowTransformer
    {
        public const int maxXPixel = 465;

        public  const int minXPixel = 175;

        public const int minYPixel = 83;

        public const int maxYPixel = 370;

        /**
         * 最小地块的宽度
         */
        public const int aLandWidth = (maxXPixel - minXPixel) / 9;

        /*
         * 最小地块的高度
         */
        public const int aLandHeight = ((maxYPixel - minYPixel) / 5);

        /**
         * 判断当前位置是否在草坪上
         */
        public static bool isOnLawn(int xLocation, int yLocation, out int column, out int row)
        {
            //可种植区域的左上角坐标 175 83
            //可种植区域的右下角坐标 464 370


            if (xLocation > minXPixel && xLocation < maxXPixel && yLocation > minYPixel && yLocation < maxYPixel)
            {
                column = (xLocation - minXPixel) / aLandWidth;

                row = (yLocation - minYPixel) / aLandHeight;
                return true;
            }

            column = 0;
            row = 0;
            return false;
        }

        /*
         * 把行列 转换为像素位置
         * 
         */
        public static void toPixels(int column, int row, out int xLocation, out int yLocation)
        {
            xLocation = column * aLandWidth + minXPixel;
            yLocation = row * aLandHeight + minYPixel;
        }
    }
}